<html>
<head><meta charset="utf-8"><title>adding-expr-before-expr · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html">adding-expr-before-expr</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="247683317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/247683317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Moore <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#247683317">(Jul 30 2021 at 08:46)</a>:</h4>
<p>I'm adding a custom statement to my compiler which needs to automatically negate the expression in front of it. I'm creating the negated expression with:</p>
<div class="codehilite"><pre><span></span><code>        let neg = self.mk_expr(
            lo.to(self.prev_token.span),
            self.mk_unary(UnOp::Not, cond),
            AttrVec::new()
        );
</code></pre></div>
<p>But when creating the final expression, I don't know which span to use, <code>self.prev_token.span</code> or <code>neg.span</code>.</p>
<p>Both work though:</p>
<div class="codehilite"><pre><span></span><code>        Ok(self.mk_expr(
                //lo.to(self.prev_token.span),
                lo.to(neg.span),
                ExprKind::If(neg, thn, els),
                attrs
        ))
</code></pre></div>
<p>Which is the correct one?</p>



<a name="247695109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/247695109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#247695109">(Jul 30 2021 at 11:22)</a>:</h4>
<p>It's up to you <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span> spans are only used for diagnostics so you could find a way to get a compile error pointing to that span and see which you like better</p>



<a name="247695460"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/247695460" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Moore <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#247695460">(Jul 30 2021 at 11:26)</a>:</h4>
<p>isn't <code>lo.to(...)</code> wrapping the span? so if I wrap <code>neg.span</code>, it's like having a reference back to that negate condition but if I wrap <code>self.prev_token.span</code> it's two spans pointing back to the same place... like imagine if I write <code>if !cond {...}</code>, doesn't compiler reference <code>cond</code> span back to <code>!</code>?</p>



<a name="247908237"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/247908237" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#247908237">(Aug 02 2021 at 10:50)</a>:</h4>
<p><span class="user-mention silent" data-user-id="429510">Dave More</span> has marked this topic as resolved.</p>



<a name="247908251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/247908251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#247908251">(Aug 02 2021 at 10:50)</a>:</h4>
<p><span class="user-mention silent" data-user-id="429510">Dave More</span> has marked this topic as unresolved.</p>



<a name="248215670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/248215670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#248215670">(Aug 03 2021 at 12:48)</a>:</h4>
<blockquote>
<p>isn't <code>lo.to(...)</code> wrapping the span?</p>
</blockquote>
<p>What do you mean by wrapping?<br>
<code>lo.to(hi)</code> will simply create a new span starting at <code>lo</code> and ending at <code>hi</code>, this new span won't "refer" to <code>lo</code> or <code>hi</code> in any way.</p>



<a name="248233346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/248233346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Moore <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#248233346">(Aug 03 2021 at 15:03)</a>:</h4>
<p>I understand, "encloses" is the right word. However I still don't understand what spans are for? Joshua already explained they are for diagnostics, but what does it mean?<br>
<span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/adding-expr-before-expr/near/247695109">said</a>:</p>
<blockquote>
<p>... spans are only used for diagnostics so you could find a way to get a compile error pointing to that span ...</p>
</blockquote>
<p>Compiler errors never show inner information, such as a span. Or do they?</p>
<p>In <code>parse_*</code> functions of <code>rustc_parse/src/parser/expr.rs</code> file:</p>
<div class="codehilite"><pre><span></span><code>let lo = self.prev_token.span;
self.mk_expr(lo.to(self.prev_token.span), ...
</code></pre></div>
<p>It seems both <code>lo</code> and <code>hi</code> are the same. What exactly is the purpose of it and how should it be used when you have a chain of expressions? Should each one be limited to itself/prev_token? or second expressions <code>lo</code> start from first one all the way to itself?</p>



<a name="248312732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/248312732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#248312732">(Aug 04 2021 at 05:38)</a>:</h4>
<p>The span is used for the <code>^^^^</code> indicators. The diagnostic machinery doesn't actually print the numerical span.</p>



<a name="248444236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/248444236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Moore <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#248444236">(Aug 05 2021 at 06:59)</a>:</h4>
<p>Thanks Joshua, one other issue and then we can mark this as resolved. What should the numerical span be for each expression? If you read my previous message, it seems in <code>rustc_parse/src/parser/expr.rs</code>, spans are set to lo/hi of <code>[prev_token.span, prev_token.span]</code>:</p>
<ol>
<li>Why is it <code>prev_token</code>, only for convenience? I'd expect something like <code>[prev_token.span, curr.span)</code></li>
<li>Should each expression only cover itself? Is there any case where a span for an expression covers multiple spans?</li>
</ol>



<a name="248724668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/248724668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Moore <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#248724668">(Aug 07 2021 at 16:44)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> polite reminder</p>



<a name="248726711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/248726711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#248726711">(Aug 07 2021 at 17:32)</a>:</h4>
<p>I don't have time to deal with this right now</p>



<a name="248726715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/adding-expr-before-expr/near/248726715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/adding-expr-before-expr.html#248726715">(Aug 07 2021 at 17:32)</a>:</h4>
<p>And also it really doesn't matter</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>